//
//  JCReportVC.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/27.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCReportVC.h"
#import "JCReportHomeCell.h"
#import "JCClubInfoSectionHeader.h"
#import "JCClubApi.h"
#import "JCReportModel.h"
#import "ClubsSetting.h"
#import "NSDate+CM_DateTools.h"
#import "JCReportWeeklyDetailVC.h"
#import "YFUserDefaults.h"

@interface JCReportVC ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray <JCReportListModel *>*reportList;
@property (weak, nonatomic) IBOutlet UIView *enptyView;
@property (weak, nonatomic) IBOutlet UIButton *viewExercisePlainBtn;

@end

@implementation JCReportVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCode];
    [self loadData];
}

- (void)initCode{
    [_tableView registerClass:[JCClubInfoSectionHeader class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([JCClubInfoSectionHeader class])];
    self.navigationItem.title = @"Sports Report";
    _reportList = [NSMutableArray array];
    _viewExercisePlainBtn.layer.cornerRadius = 3;
    _viewExercisePlainBtn.layer.masksToBounds = YES;
}

- (void)loadData{
    __weak typeof(self)weakSelf = self;
    [self.hud showWithMaskType:WSProgressHUDMaskTypeClear];
    [JCClubApi getReportListForUser:_userID complete:^(NSError *error, NSArray<JCReportListModel *> *reportList) {
        if (error) {
            [weakSelf.hud showErrorWithString:error.domain dimissAfterSecond:2];
            if (error.code == -30009) {
                weakSelf.enptyView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }else{
                weakSelf.enptyView.hidden = YES;
                weakSelf.tableView.hidden = NO;
            }
        }else{
            [weakSelf.hud dismiss];
            [weakSelf.reportList removeAllObjects];
            [weakSelf.reportList addObjectsFromArray:reportList];
            [weakSelf.tableView reloadData];
            if (reportList.count == 0) {
                weakSelf.enptyView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }else{
                weakSelf.enptyView.hidden = YES;
                weakSelf.tableView.hidden = NO;
            }
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _reportList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JCReportHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JCReportHomeCell class]) forIndexPath:indexPath];
    cell.dailyModels = _reportList[indexPath.section].dailyModels.mutableCopy;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kSectionHeaderHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    JCClubInfoSectionHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([JCClubInfoSectionHeader class])];
    [header setleftTitle:@"Weekly"];
    [header setRightTitle:[NSDate timeConversionYear:_reportList[section].year.integerValue WeakOfYear:_reportList[section].yearWeek.integerValue]];
    return header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    [self performSegueWithIdentifier:kSegueWeeklyDetail sender:[tableView cellForRowAtIndexPath:indexPath]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _tableView) {
        CGFloat sectionHeaderHeight = kSectionHeaderHeight;
        if (scrollView == _tableView) {
            if (scrollView.contentOffset.y<=sectionHeaderHeight && scrollView.contentOffset.y>=0) {
                scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
                scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)gotoMeAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    [YFUserDefaults saveClubOnMEState:YES];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:kSegueWeeklyDetail]) {
        JCReportWeeklyDetailVC *vc = [segue destinationViewController];
        vc.userID = _userID?_userID:[User getUseInfo].userId;
        if ([[User getUseInfo] clubUserType] == ClubUserTypeAdmin) {
            vc.clubID = [User getUseInfo].adminClubs.firstObject.clubID;
        }else if ([[User getUseInfo] clubUserType] == ClubUserTypeGeneral) {
            vc.clubID = [User getUseInfo].joinClubs.firstObject.clubID;
        }
        vc.reportListModel = _reportList[[_tableView indexPathForCell:sender].section];
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
